package com.bert.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bert.entity.SkyEntity;
import com.bert.entity.SkyWalking.*;
import com.bert.entity.SkyWalking.TraceQuery.ConditionEntity;
import com.bert.entity.SkyWalking.TraceQuery.PagingEntity;
import com.bert.entity.SkyWalking.TraceQuery.QueryDurationEntity;
import com.bert.entity.SkyWalking.TraceQuery.TraceQueryEntity;
import com.bert.feign.SkyWalkingClient;
import com.bert.service.SkyWalkingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/sky")
public class SkyWalkingController {

    @Resource
    private SkyWalkingService skyWalkingService;

    @PostMapping("/test")
    public void post(@RequestBody List<SkyEntity> skyEntityList) {
        String jsonString = JSON.toJSONString(skyEntityList);
        log.info("告警信息");
        log.info(jsonString);
    }

    @GetMapping("/getServices")
    public Object getServices(String startTime, String endTime, Long timeOut, Integer pageNum, Integer pageSize){
        //        String startTime = "2024-06-13 0904";
        //        String endTime = "2024-06-13 0934";
        if (Objects.isNull(pageNum)) {
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)) {
            pageSize = 20;
        }
        return skyWalkingService.getTagInfoList(startTime, endTime, timeOut, pageNum, pageSize);
    }

    @GetMapping("/closeServices")
    public List<ServiceInfoEntity> closeServices(String startTime, String endTime, Long timeOut, Integer pageNum, Integer pageSize){
        if (Objects.isNull(pageNum)) {
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)) {
            pageSize = 20;
        }
        List<TagInfoEntity> tagInfoList = skyWalkingService.getTagInfoList(startTime, endTime, timeOut, pageNum, pageSize);
        List<ServiceInfoEntity> serviceInfos = new ArrayList<>();
        tagInfoList.forEach(e ->{
            String service = e.getService();
            String serviceName = service.contains("::")? service.split("::")[1] : "";
            e.getChild().forEach(c ->{
                if (Objects.nonNull(c.getUrl())) {
                    // 关闭子服务
                    serviceInfos.add(new ServiceInfoEntity(service, serviceName, c.getIp(), c.getPort()));
                } else {
                    // 关闭父服务
                    serviceInfos.add(new ServiceInfoEntity(service, serviceName, e.getIp(), e.getPort()));
                }
            });
        });
        return this.close(serviceInfos);
    }

    private List<ServiceInfoEntity> close(List<ServiceInfoEntity> serviceInfos){
        List<ServiceInfoEntity> list = serviceInfos.stream().distinct().toList();
        // 去重后处理
        list.forEach(e->{
            if (e.getIp().equals("localhost")) {
                e.setIp(this.getLocalhost());
            }
            log.info("即将关闭的服务IP => " + e.getIp() + " 端口 => " + e.getPort());
        });
        return list;
    }

    private String getLocalhost(){
        String ip = "1";
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        } catch (UnknownHostException ex) {
            throw new RuntimeException(ex);
        }
        return ip;
    }




}
